In computer science, comparator networks are abstract devices built up of a fixed number of "wires", carrying values, and comparator modules that connect pairs of wires, swapping the values on the wires if they are not in a desired order. Such networks are typically designed to perform sorting on fixed numbers of values, in which case they are called sorting networks.
Sorting networks differ from general in that they are not capable of handling arbitrarily large inputs, and in that their sequence of comparisons is set in advance, regardless of the outcome of previous comparisons. In order to sort a larger number of inputs, new sorting networks must be constructed. This independence of comparison sequences is useful for parallel execution and for implementation in hardware. Despite the simplicity of sorting nets, their theory is surprisingly deep and complex. Sorting networks were first studied circa 1954 by Armstrong, Nelson and O'Connor, who subsequently patented the idea.
Sorting networks can be implemented either in hardware or in software. Donald Knuth describes how the comparators for binary integers can be implemented as simple, three-state electronic devices. Ken Batcher, in 1968, suggested using them to construct switch for computer hardware, replacing both buses and the faster, but more expensive, . Since the 2000s, sorting nets (especially bitonic sorter) are used by the GPGPU community for constructing sorting algorithms to run on graphics processing units.
In a formula, if the top wire carries and the bottom wire carries , then after hitting a comparator the wires carry and , respectively, so the pair of values is sorted. A network of wires and comparators that will correctly sort all possible inputs into ascending order is called a sorting network or Kruskal hub. By reflecting the network, it is also possible to sort all inputs into descending order.
The full operation of a simple sorting network is shown below. It is evident why this sorting network will correctly sort the inputs; note that the first four comparators will "sink" the largest value to the bottom and "float" the smallest value to the top. The final comparator sorts out the middle two wires.
| ]] |
The insertion network (or equivalently, bubble network) has a depth of , where is the number of values. This is better than the time needed by random-access machines, but it turns out that there are much more efficient sorting networks with a depth of just , as described below.
The zero-one principle states that, if a sorting network can correctly sort all sequences of zeros and ones, then it is also valid for arbitrary ordered inputs. This not only drastically cuts down on the number of tests needed to ascertain the validity of a network, it is of great use in creating many constructions of sorting networks as well.
The principle can be proven by first observing the following fact about comparators: when a monotonically increasing function is applied to the inputs, i.e., and are replaced by and , then the comparator produces and . By induction on the depth of the network, this result can be extended to a lemma stating that if the network transforms the sequence into , it will transform into . Suppose that some input contains two items , and the network incorrectly swaps these in the output. Then it will also incorrectly sort for the function
1\ &\mbox{if } x > a_i \\
0\ &\mbox{otherwise.}
\end{cases}
This function is monotonic, so we have the zero-one principle as the Contraposition.
It is also possible to construct networks of depth (hence size ) using a construction called the AKS network, after its discoverers Ajtai, Komlós, and Szemerédi. While an important theoretical discovery, the AKS network has very limited practical application because of the large linear constant hidden by the Big-O notation. These are partly due to a construction of an expander graph.
A simplified version of the AKS network was described by Paterson in 1990, who noted that "the constants obtained for the depth bound still prevent the construction being of practical value".
A more recent construction called the zig-zag sorting network of size was discovered by Goodrich in 2014.
For larger networks neither the optimal depth nor the optimal size are currently known. The bounds known so far are provided in the table below:
The first sixteen depth-optimal networks are listed in Knuth's Art of Computer Programming, Section 5.3.4: Networks for Sorting. and have been since the 1973 edition; however, while the optimality of the first eight was established by Floyd and Knuth in the 1960s, this property wasn't proven for the final six until 2014
For one to twelve inputs, minimal (i.e. size-optimal) sorting networks are known, and for higher values, lower bounds on their sizes can be derived inductively using a lemma due to Van Voorhis (p. 240): . The first ten optimal networks have been known since 1969, with the first eight again being known as optimal since the work of Floyd and Knuth, but optimality of the cases and took until 2014 to be resolved. The optimality of the smallest known sorting networks for and was resolved in 2020.
Some work in designing optimal sorting network has been done using genetic algorithms: D. Knuth mentions that the smallest known sorting network for was found by Hugues Juillé in 1995 "by simulating an evolutionary process of genetic breeding" (p. 226), and that the minimum depth sorting networks for and were found by Loren Schwiebert in 2001 "using genetic methods" (p. 229).
|
|